package com.coincalf.exchange.service;

import com.baomidou.mybatisplus.service.IService;
import com.coincalf.exchange.entity.Account;
import com.coincalf.exchange.entity.EntrustOrder;
import com.coincalf.exchange.entity.Market;
import com.coincalf.exchange.entity.TurnoverOrder;
import com.coincalf.framework.enums.BusinessType;
import com.coincalf.framework.exception.AccountException;

import java.math.BigDecimal;

/**
 * <p>
 * 用户财产记录 服务类
 * </p>
 *
 * @author qiang
 * @since 2018-05-12
 */
public interface AccountService extends IService<Account> {

    /**
     * 查询资金账户
     *
     * @param userId 用户ID
     * @param coinId 币种ID
     * @return
     */
    Account queryByUserIdAndCoinId(long userId, long coinId);

    /**
     * 币币交易撮合完成后修改资金账户
     *
     * @param dealOrder 成交订单
     * @param buyOrder  委托买单
     * @param sellOrder 委托卖单
     * @param buyLock   买单需要重新冻结资金额度
     * @param sellLock  卖单需要重新冻结资金额度
     * @throws AccountException
     */
    void tradeMatchModifyAccount(
            TurnoverOrder dealOrder,
            EntrustOrder buyOrder,
            EntrustOrder sellOrder,
            BigDecimal buyLock,
            BigDecimal sellLock
    ) throws AccountException;

    /**
     * 创新交易撮合完成后修改资金账户
     *
     * @param market    交易对
     * @param dealOrder 成交订单
     * @param buyOrder  委托买单
     * @param sellOrder 委托卖单
     * @param buyLock   买单需要重新冻结资金额度
     * @param sellLock  卖单需要重新冻结资金额度
     * @throws AccountException
     */
    void forexMatchModifyAccount(Market market,
                                 TurnoverOrder dealOrder,
                                 EntrustOrder buyOrder,
                                 EntrustOrder sellOrder,
                                 BigDecimal buyLock,
                                 BigDecimal sellLock) throws AccountException;

    /**
     * 冻结资金
     *
     * @param userId       用户ID
     * @param coinId       币种ID
     * @param amount       冻结金额
     * @param businessType 业务类型
     * @param orderId      关联订单号
     * @return
     */
    boolean lockAmount(long userId,
                       long coinId,
                       BigDecimal amount,
                       BusinessType businessType,
                       long orderId) throws AccountException;

    /**
     * 解冻资金
     *
     * @param userId       用户ID
     * @param coinId       币种ID
     * @param amount       冻结金额
     * @param businessType 业务类型
     * @param orderId      关联订单号
     * @return
     */
    boolean unlockAmount(long userId,
                         long coinId,
                         BigDecimal amount,
                         BusinessType businessType,
                         long orderId) throws AccountException;

    /**
     * 增加资金(平仓盈亏)
     *
     * @param userId       用户ID
     * @param coinId       币种ID
     * @param amount       冻结金额
     * @param businessType 业务类型
     * @param remark       备注
     * @param orderId      关联订单号
     * @return
     */
    boolean addProfit(long userId,
                      long coinId,
                      BigDecimal amount,
                      BusinessType businessType,
                      String remark,
                      long orderId) throws AccountException;

    /**
     * 资金划转
     *
     * @param fromUserId   转出用户ID
     * @param toUserId     转入用户ID
     * @param coinId       币种ID
     * @param amount       金额
     * @param businessType 业务类型
     * @param orderId      关联订单号
     * @param remark       备注
     * @return
     */
    boolean transferAmount(long fromUserId,
                           long toUserId,
                           long coinId,
                           BigDecimal amount,
                           BusinessType businessType,
                           long orderId,
                           String remark) throws AccountException;
}
